﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.Specialized;
using SQLToolbox.Types;
namespace SQLToolbox.View.UserControls
{
	/// <summary>
	/// Interaction logic for TextComparer.xaml
	/// </summary>
	public partial class TextComparer : UserControl
	{
		ScriptComparisonResult res;
		SolidColorBrush leftBrush = new SolidColorBrush(Color.FromRgb(78,148,194));
		SolidColorBrush rightBrush = new SolidColorBrush(Color.FromRgb(255,205,45));

		public TextComparer()
		{
			InitializeComponent();
			rbComp.Checked += new RoutedEventHandler(RadioButton_Checked);
			rbLeft.Checked += new RoutedEventHandler(RadioButton_Checked);
			rbRight.Checked += new RoutedEventHandler(RadioButton_Checked);
		}

		public void Clear()
		{
			res = null;
			table.RowGroups[0].Rows.Clear();
		}
		
		public void Show(ScriptComparisonResult sccr)
		{
			res = sccr;
			Show();
		}



		private void Show()
		{
            if (rbComp.IsChecked == true)
                //ShowTextComparisonWithMissingRows();
                ShowTextComparison();
            else if (res == null)
                return;
            else if (rbLeft.IsChecked == true)
                ShowText(res.Left);
            else if (rbRight.IsChecked == true)
                ShowText(res.Right);
		}

		private void ShowText(IEnumerable<string> sc)
		{
			table.RowGroups[0].Rows.Clear();

			if (sc != null)
			{
				int index = 0;
				sc.All((s) =>
				{
					AddRowToRowGroup(table.RowGroups[0], string.Format("{0}", index++), string.Empty, s);
					return true;
				});
			}
		}

		private void ShowTextComparison()
		{
			table.RowGroups[0].Rows.Clear();

			for (int index = 0; index < res.Count; index++)
			{
				if ((string.IsNullOrEmpty(res[index].Left)) &&
					(string.IsNullOrEmpty(res[index].Right)))
				{
					continue;
				}
				else if (string.IsNullOrEmpty(res[index].Left))
				{
					//Only right string exists
					AddRowToRowGroup(table.RowGroups[0], string.Format("{0}", index), "<", res[index].Right, rightBrush);
				}
				else if (string.IsNullOrEmpty(res[index].Right))
				{
					//Only left string exists
					AddRowToRowGroup(table.RowGroups[0], string.Format("{0}", index), ">", res[index].Left, leftBrush);
				}
				else
				{
					//both exist
					if (res[index].DiffIndex != -1)
					{	//strings differ
						AddRowToRowGroup(table.RowGroups[0], string.Format("{0}", index), ">",
										res[index].Left, res[index].DiffIndex, leftBrush);
						AddRowToRowGroup(table.RowGroups[0], " ", "<",
							res[index].Right, res[index].DiffIndex, rightBrush);
					}
					else
					{
						//Strings equal
						AddRowToRowGroup(table.RowGroups[0], string.Format("{0}", index), string.Empty, res[index].Left);
						
					}
				}
			}
		}

		private static void AddRowToRowGroup(TableRowGroup rowGroup, string rowNum, string prefix, string text, int colorFromIndex, Brush brush)
		{
			TableRow row = new TableRow();
			TableCell c = new TableCell(new Paragraph(new Run(rowNum)));
			//c.Background = brush;
			row.Cells.Add(c);
			c = new TableCell(new Paragraph(new Run(prefix)));
			c.Background = brush;
			row.Cells.Add(c);
			Paragraph p = new Paragraph();
			p.Inlines.Add(new Run(text.Substring(0, colorFromIndex)));

			Run colored = new Run(text.Substring(colorFromIndex));
			colored.Background = brush;
			p.Inlines.Add(colored);
			c = new TableCell(p);
			row.Cells.Add(c);
			rowGroup.Rows.Add(row);
		}


		private static void AddRowToRowGroup(TableRowGroup rowGroup, string rowNum, string prefix, string text, Brush brush)
		{
			TableRow row = new TableRow();
			TableCell c = new TableCell(new Paragraph(new Run(rowNum)));
			//c.Background = brush;
			row.Cells.Add(c);
			c = new TableCell(new Paragraph(new Run(prefix)));
			c.Background = brush;
			row.Cells.Add(c);
			c = new TableCell(new Paragraph(new Run(text)));
			c.Background = brush;
			row.Cells.Add(c);
			rowGroup.Rows.Add(row);
		}


		private static void AddRowToRowGroup(TableRowGroup rowGroup, string rowNum, string prefix, string text)
		{
			TableRow row = new TableRow();
			TableCell c = new TableCell(new Paragraph(new Run(rowNum)));
			row.Cells.Add(c);
			c = new TableCell(new Paragraph(new Run(prefix)));
			row.Cells.Add(c);
			c = new TableCell(new Paragraph(new Run(text)));
			row.Cells.Add(c);
			rowGroup.Rows.Add(row);


		}


		private static void AddRowToRowGroup(TableRowGroup rowGroup, string rowNum, string text)
		{
			TableRow row = new TableRow();
			TableCell c = new TableCell(new Paragraph(new Run(rowNum)));
			row.Cells.Add(c);
			c = new TableCell(new Paragraph(new Run(string.Empty)));
			row.Cells.Add(c);
			c = new TableCell(new Paragraph(new Run(text)));
			row.Cells.Add(c);
			rowGroup.Rows.Add(row);
		}

		private void RadioButton_Checked(object sender, RoutedEventArgs e)
		{
			Show();
		}

		private int FirstDifferenceIndex(string a, string b, bool ignoreCase)
		{
			var _a = (ignoreCase) ? a.ToLower() : a;
			var _b = (ignoreCase) ? b.ToLower() : b;

			int i = 0;
			for (; i < a.Length && i < b.Length; i++)
			{
				if (a[i] != b[i])
					return i;
			}
			return i;
		}
	}
}
